const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')

function resolve (...src) {
  return path.resolve(__dirname, ...src)
}

/**
 * 生成各个部分的HTML
 * @returns
 */
function createHtmlTemplate () {
  const htmlMap = ['index']
  return htmlMap.map((item) => new HtmlWebpackPlugin({
    filename: item + '.html',
    template: './template/background.ejs',
    inject: 'body',
    chunks: [item]
  }))
}


function handleOnBefore (app) {

}

function hanleOnAfter (app) {

}

module.exports = {
  mode: 'development',
  entry: {
    index: './index.tsx'
  },
  output: {
    filename: '[name].js',
    path: resolve('dist'),
    publicPath: '/'
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js', '.jsx'],
    alias: {
      '@': resolve('src'),

    },
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.scss$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader' },
          { loader: 'sass-loader' }
        ]
      },
      {
        test: /\.(png|jpe?g|gif)$/i,
        use: [
          {
            loader: 'file-loader',
          },
        ],
      },
    ]
  },
  devServer: {
    hot: true,
    historyApiFallback: true,
  },
  devtool: 'inline-source-map',
  plugins: [
    new CleanWebpackPlugin({}),
    new CopyWebpackPlugin({
      patterns: [
        { from: 'src/assets', to: 'assets' },
      ]
    }),
    ...createHtmlTemplate(),
    new webpack.HotModuleReplacementPlugin()
  ],
}
